package com.sangfor.ssl.service.utils.network;

import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.setting.GlobalSettingUtils;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.utils.IGeneral;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

/* loaded from: classes2.dex */
public class HttpHeadReader {
    private static final String TAG = "HttpHeadReader";
    private String mBody;
    private final HttpConnect mConnection;
    private int mLen;
    private String mLocation;
    private int mStatusCode;

    public HttpHeadReader() {
        this.mStatusCode = 0;
        this.mLen = 0;
        this.mConnection = new HttpConnect();
    }

    public HttpHeadReader(HttpConnect httpConnect) {
        this.mStatusCode = 0;
        this.mLen = 0;
        this.mConnection = httpConnect;
    }

    public void parseHttpHeaderN(String str) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
        String str2 = null;
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                return;
            }
            String trim = readLine.trim();
            if (trim.startsWith("HTTP/1.")) {
                this.mStatusCode = Integer.parseInt(trim.split("\\s+")[1]);
            } else if (trim.startsWith("Set-Cookie: ")) {
                str2 = trim.split(": ")[1];
            } else if (trim.startsWith("Location: ")) {
                this.mLocation = trim.split(": ")[1];
            } else if (trim.startsWith("Content-Length: ")) {
                this.mLen = Integer.parseInt(trim.split(": ")[1]);
            } else if (trim.equals("")) {
                return;
            }
            if (str2 != null) {
                String[] split = str2.split(";\\s*");
                String str3 = (String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_HOST_KEY);
                boolean z = false;
                String str4 = null;
                for (String str5 : split) {
                    String[] split2 = str5.split("=");
                    if (split2.length == 2) {
                        this.mConnection.setCookie(str3, split2[0], split2[1]);
                        if (IGeneral.COOKIE_MBTWFID_STRING.equals(split2[0])) {
                            str4 = split2[1];
                            z = true;
                        }
                    }
                }
                if (z) {
                    this.mConnection.setCookie(str3, IGeneral.COOKIE_MBTWFID_STRING, str4);
                    this.mConnection.setCookie(str3, IGeneral.COOKIE_TWFID_STRING, str4);
                } else {
                    HttpConnect httpConnect = this.mConnection;
                    httpConnect.setCookie(str3, IGeneral.COOKIE_MBTWFID_STRING, httpConnect.getCookie(str3, IGeneral.COOKIE_TWFID_STRING));
                }
                GlobalSettingUtils.updateTwfid(this.mConnection.getCookie(str3, IGeneral.COOKIE_TWFID_STRING));
            }
        }
    }

    public String parseHttpHeader(String str) throws IOException {
        String str2;
        String str3;
        BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
        String str4 = null;
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                break;
            }
            String trim = readLine.trim();
            if (trim.startsWith("HTTP/1.")) {
                this.mStatusCode = Integer.parseInt(trim.split("\\s+")[1]);
            } else if (trim.startsWith("Set-Cookie: ")) {
                str4 = trim.split(": ")[1];
            } else if (trim.startsWith("Location: ")) {
                this.mLocation = trim.split(": ")[1];
            } else if (trim.startsWith("Content-Length: ")) {
                this.mLen = Integer.parseInt(trim.split(": ")[1]);
            } else if (trim.equals("")) {
                break;
            }
            if (str4 != null) {
                String[] split = str4.split(";\\s*");
                String str5 = (String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_HOST_KEY);
                int length = split.length;
                int i = 0;
                boolean z = false;
                String str6 = null;
                while (i < length) {
                    String[] split2 = split[i].split("=");
                    if (split2.length == 2) {
                        str3 = str4;
                        this.mConnection.setCookie(str5, split2[0], split2[1]);
                        if (IGeneral.COOKIE_MBTWFID_STRING.equals(split2[0])) {
                            str6 = split2[1];
                            z = true;
                        }
                    } else {
                        str3 = str4;
                    }
                    i++;
                    str4 = str3;
                }
                str2 = str4;
                Log.debug(TAG, trim);
                if (z) {
                    this.mConnection.setCookie(str5, IGeneral.COOKIE_MBTWFID_STRING, str6);
                    this.mConnection.setCookie(str5, IGeneral.COOKIE_TWFID_STRING, str6);
                } else {
                    HttpConnect httpConnect = this.mConnection;
                    httpConnect.setCookie(str5, IGeneral.COOKIE_MBTWFID_STRING, httpConnect.getCookie(str5, IGeneral.COOKIE_TWFID_STRING));
                }
                GlobalSettingUtils.updateTwfid(this.mConnection.getCookie(str5, IGeneral.COOKIE_TWFID_STRING));
            } else {
                str2 = str4;
            }
            str4 = str2;
        }
        StringBuilder sb = new StringBuilder();
        char[] cArr = new char[4096];
        while (true) {
            int read = bufferedReader.read(cArr, 0, 4096);
            if (read != -1) {
                sb.append(cArr, 0, read);
            } else {
                String sb2 = sb.toString();
                this.mBody = sb2;
                return sb2;
            }
        }
    }

    public int statusCode() {
        return this.mStatusCode;
    }

    public String httpBody() {
        return this.mBody;
    }

    public String httpLocation() {
        int i = this.mStatusCode;
        if (i == 301 || i == 302) {
            return this.mLocation;
        }
        return null;
    }
}
